/*
 * C/C++ KLIB Project
 * 
 * Copyright (c) 2012 Ken Zhang (cpp.china@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 */


int compare_int(const void *v1, const void *v2)
{
	if (v1 != v2) {
		if (*((int *)v1) > *((int *)v2))
			return 1;
		else if (*((int *)v1) < *((int *)v2))
			return -1;
	}
	return 0;
}

int compare_int_r(const void *v1, const void *v2)
{
	return -compare_int(v1, v2);
}

int compare_long(const void *v1, const void *v2)
{
	if (v1 != v2) {
		if (*((long *)v1) > *((long *)v2))
			return 1;
		else if (*((long *)v1) < *((long *)v2))
			return -1;
	}
	return 0;
}

int compare_long_r(const void *v1, const void *v2)
{
	return -compare_int(v1, v2);
}

int compare_double(const void *v1, const void *v2)
{
	if (v1 != v2) {
		if (*((double *)v1) > *((double *)v2))
			return 1;
		else if (*((double *)v1) < *((double *)v2))
			return -1;
	}
	return 0;
}

int compare_double_r(const void *v1, const void *v2)
{
	return -compare_double(v1, v2);
}

int compare_longdouble(const void *v1, const void *v2)
{
	if (v1 != v2) {
		if (*((long double *)v1) > *((long double *)v2))
			return 1;
		else if (*((long double *)v1) < *((long double *)v2))
			return -1;
	}
	return 0;
}

int compare_longdouble_r(const void *v1, const void *v2)
{
	return -compare_longdouble(v1, v2);
}

void *max2(void *a, void *b, int (*compare)(const void *, const void *))
{
	if (compare(a, b) > 0)
		return a;
	else
		return b;
}

void *max3(void *a, void *b, void *c, int (*compare)(const void *, const void *))
{
	if (compare(a, b) > 0)
		return max2(a, c, compare);
	else
		return max2(b, c, compare);
}

void *min2(void *a, void *b, int (*compare)(const void *, const void *))
{
	if (compare(a, b) < 0)
		return a;
	else
		return b;
}

void *min3(void *a, void *b, void *c, int (*compare)(const void *, const void *))
{
	if (compare(a, b) < 0)
		return min2(a, c, compare);
	else
		return min2(b, c, compare);
}

void *mid3(void *a, void *b, void *c, int (*compare)(const void *, const void *))
{
	if (compare(a, b) > 0) {
		if (compare(b, c) > 0) {		// a > b > c
			return b;
		}
		else if (compare(a, c) > 0) {	// a > c > b
			return c;
		}
		else {							// c > a > b
			return a;
		}
	}
	else {
		if (compare(a, c) > 0) {		// b > a > c
			return a;
		}
		else if (compare(b, c) > 0) {	// b > c > a
			return c;
		}
		else {							// a > c > b
			return b;
		}
	}
}

int midx(void *base, int i, int j, int k, void *(*get_element)(void *, int), int (*compare)(const void *, const void *))
{
	void *a = get_element(base, i);
	void *b = get_element(base, j);
	void *c = get_element(base, k);

	if (compare(a, b) > 0) {
		if (compare(b, c) > 0) {		// a > b > c
			return j;
		}
		else if (compare(a, c) > 0) {	// a > c > b
			return k;
		}
		else {							// c > a > b
			return i;
		}
	}
	else {
		if (compare(a, c) > 0) {		// b > a > c
			return i;
		}
		else if (compare(b, c) > 0) {	// b > c > a
			return k;
		}
		else {							// a > c > b
			return j;
		}
	}
}
